/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.PropChangeHandler;
import net.worlds.net.RemotePlayer;
import org.jbox2d.common.Vec2D;


public class Connection extends GameObject{
    private RemotePlayer client;
    private Player player;
    private long lastMoveTime = 0;
    public Connection() throws WorldsException
    {
        super("Connection");
        addPropChangeHandler("Player", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                player = (Player) (GameObject) value;
                client.sendPlayer((Player)value);
            }
        });
        //x_;
    }
    public RemotePlayer getClient()
    {
        return client;
    }
    @Override
    public void destructor() throws WorldsException
    {
        super.destructor();
        if(client != null)
            client.disconnect();
        if(player != null)
            player.remove();
    }
    public void moveCharacter(final Vec2D pos, final Vec2D vel)
    {
        try {
            if (player == null) {
                return;
            }
            final GameObject character = player.getObjectProp("Character");
            if (character == null || !(character instanceof Character))
                return;
            if(!character.hasChild("Body") || !(character.getChild("Body") instanceof Part))
                return;
            final Part body = (Part) character.getChild("Body");
            Main.runInMainThread(new Runnable()
            {
                public void run() {
                    try {
                        //body.setTargetPoint(pos);
                        body.setPosition(pos);
                        if(!vel.equals(Vec2D.ORIGIN))
                            body.noFriction = true;
                        else
                            body.noFriction = false;
                        if(body.hasChild("Controller") && body.getChild("Controller") instanceof VelocityController && vel.y == 0)
                            ((VelocityController)body.getChild("Controller")).setProp("Velocity", vel, true);
                        else
                            body.getBody().setLinearVelocity(vel);
                    } catch (WorldsException ex) {
                        ex.printStackTrace();
                    }
                }
            });
            if(body.getBody().m_actualPosition != null)
            {
                int e = (int) (System.currentTimeMillis() - lastMoveTime);
                lastMoveTime = System.currentTimeMillis();
            }
            body.getBody().m_actualPosition = pos;
        } catch (WorldsException ex) {
            ex.printStackTrace();
        }
    }
    public Player getPlayer()
    {
        return player;
    }
    public void setClient(RemotePlayer r)
    {
        client = r;
    }
}
